#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
int a[N];
bool cmp(string s, string t) {  // 大于
  if (s.size() != t.size()) return s.size() > t.size();
  return s > t;
}
bool check(string ip) {
  auto pos = ip.find(':');
  if (pos == string::npos) return false;
  ip[pos] = '.';
  if (ip.find(':') != string::npos) return false;
  if (ip.back() == '.') return false;
  vector<string> ss;
  while (ip.size()) {
    pos = ip.find('.');
    if (pos == string::npos) {
      ss.push_back(ip);
      break;
    }
    ss.push_back(ip.substr(0, pos));
    ip = ip.substr(pos + 1);
  }
  if (ss.size() != 5) return false;
  for (auto s : ss)
    if (s.size() == 0 || s.size() > 1 && s[0] == '0') return false;
  string e = ss.back();
  if (cmp(e, "65535")) return false;
  ss.pop_back();
  for (auto s : ss) {
    if (cmp(s, "255")) return false;
  }
  return true;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  string s, ip;
  map<string, int> servers;
  rep(i, 1, n) {
    cin >> s >> ip;
    if (!check(ip))
      cout << "ERR" << endl;
    else if (s == "Server") {
      if (servers.count(ip))
        cout << "FAIL" << endl;
      else {
        servers[ip] = i;
        cout << "OK" << endl;
      }
    } else {
      if (!servers.count(ip))
        cout << "FAIL" << endl;
      else
        cout << servers[ip] << endl;
    }
  }
  return 0;
}